package xyz.ibenben.zhongdian.system.controller.sys;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import com.github.pagehelper.*;

import net.sf.json.JSONArray;
import xyz.ibenben.zhongdian.common.annotation.SystemControllerLog;
import xyz.ibenben.zhongdian.common.constants.Constants;
import xyz.ibenben.zhongdian.system.controller.BaseController;
import xyz.ibenben.zhongdian.system.entity.sys.*;
import xyz.ibenben.zhongdian.system.form.sys.SysRoleForm;
import xyz.ibenben.zhongdian.system.service.sys.*;

/**
 * 系统角色控制类
 * 提供了一些基本的服务，如初始化、获取角色列表等方法。
 * spring mvc思想 通过controller来调用service里的方法，
 * service里的方法再通过调用Dao来实现对数据的操作
 * 返回值统统是String类型的返回页面有统一的安排方便代码提取
 *
 * @author chenjian
 * @since 2017年9月27日上午10:57:28
 */
@Controller
@SessionAttributes("sysRoleForm")
@RequestMapping("/sys/sysRole")
public class RoleController extends BaseController{
	/* 实体名称 */
	private static final String ENTITYNAME = "sysRole";

	private final SysRoleService roleService;
	private final SysRoleResourcesService roleResourcesService;
	private final SysResourcesService resourcesService;

	@Autowired
	public RoleController(SysRoleService roleService, SysRoleResourcesService roleResourcesService, SysResourcesService resourcesService) {
		this.roleService = roleService;
		this.roleResourcesService = roleResourcesService;
		this.resourcesService = resourcesService;
	}

	/**
	 * 初始化
	 * @param map 参数
	 * @param sysRoleForm 条件参数
	 */
	@ModelAttribute
	public void init(Model map, @ModelAttribute SysRoleForm sysRoleForm){
		SysRoleForm newSysRoleForm = new SysRoleForm();
		if(sysRoleForm != null){
			newSysRoleForm = sysRoleForm;
		}
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), newSysRoleForm);
	}

	/**
	 * 获取角色列表
	 * @param map 参数
	 * @param sysRoleForm 条件参数
	 * @return 页面
	 */
	@RequestMapping(value="/getall")
	@SystemControllerLog(description = "获取角色列表")
	public String getall(Model map, @ModelAttribute SysRoleForm sysRoleForm){
		map.addAttribute(String.format(Constants.FORMNAME, ENTITYNAME), sysRoleForm);
		Page<SysRole> page = PageHelper.startPage(sysRoleForm.getPageIndex(), sysRoleForm.getPageSize());
		List<SysRole> list = roleService.selectByExample(sysRoleForm.buildSearchCommond());
		map.addAttribute(String.format(Constants.LISTENTITY, ENTITYNAME), list);
		map.addAttribute(Constants.PI, page.toPageInfo());
		return "sys/" + String.format(Constants.LISTPAGE, ENTITYNAME);
	}

	/**
	 * 删除角色记录
	 * @param id 主键
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping(value = "/delete")
	@SystemControllerLog(description = "删除角色记录")
	public String delete(@RequestParam("id")Long id, HttpServletRequest request) {
		roleService.delete(id, request);
		return String.format(Constants.REDIRECT, "sys/" + ENTITYNAME);
	}

	/**
	 * 删除角色列表
	 * @param sysRoleForm 条件参数
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping("/deleteall")
	@SystemControllerLog(description = "删除角色列表")
	public String deleteall(@ModelAttribute SysRoleForm sysRoleForm,
			HttpServletRequest request) {
		if(sysRoleForm.getSelectIds() != null){
			String[] ids = sysRoleForm.getSelectIds().split(Constants.COMMA);
			for(String id: ids){
				roleService.delete(Long.parseLong(id), request);
			}
		}
		return String.format(Constants.REDIRECT, "sys/" + ENTITYNAME);
	}

	/**
	 * 打开角色添加页面
	 * @param map 参数
	 * @return 页面
	 */
	@RequestMapping(value="/add", method= RequestMethod.GET)
	@SystemControllerLog(description = "打开角色添加页面")
	public String openAddPage(Model map){
		map.addAttribute(ENTITYNAME, new SysRole());
		return "sys/" + String.format(Constants.ADDPAGE, ENTITYNAME);
	}

	/**
	 * 保存角色添加记录
	 * @param t 实体
	 * @param bindingResult 校验
	 * @param request 请求
	 * @return 页面
	 */
	@RequestMapping(value="/add", method= RequestMethod.POST)
	@SystemControllerLog(description = "保存角色添加记录")
	public String postAddMessage(@Valid @ModelAttribute(ENTITYNAME) SysRole t,
			BindingResult bindingResult, HttpServletRequest request){
		SysRole role = roleService.selectByRoleDesc(t.getRoleDesc());
		if(null != role){
			bindingResult.rejectValue("roleDesc", "misFormat", "这个角色名称已经存在了！请更改!");
			return "sys/" + String.format(Constants.ADDPAGE, ENTITYNAME);
		}
		if (!bindingResult.hasErrors()) {
			roleService.save(t, request);
			return String.format(Constants.REDIRECT, "sys/" + ENTITYNAME);
		} else {
			return "sys/" + String.format(Constants.ADDPAGE, ENTITYNAME);
		}
	}

	/**
	 * 浏览角色记录
	 * @param map 参数
	 * @param id 主键
	 * @param sysRoleForm 条件参数
	 * @return 页面
	 */
	@RequestMapping(value="/view", method= RequestMethod.GET)
	@SystemControllerLog(description = "浏览角色记录")
	public String openViewPage(Model map, @RequestParam Integer id,
			@ModelAttribute SysRoleForm sysRoleForm){
		map.addAttribute(ENTITYNAME, roleService.selectByKey(id));
		return "sys/" + String.format(Constants.VIEWPAGE, ENTITYNAME);
	}

	/**
	 * 打开获取资源列表
	 * @param id 主键
	 * @param map 参数
	 * @return 页面
	 */
	@RequestMapping("/resources")
	@SystemControllerLog(description = "打开获取资源列表")
	public String resources(@RequestParam Long id, Model map){
		map.addAttribute("roleId", id);
		List<SysResources> resourcesList = resourcesService.queryResourcesListWithSelected(id);
		map.addAttribute("resourcesList", JSONArray.fromObject(resourcesList));
		return "sys/sysRole/resources";
	}

	/**
	 * 分配角色
	 * @param roleId 参数
	 * @param resourcesId 参数
	 * @return 页面
	 */
	@RequestMapping("/saveRoleResources")
	@SystemControllerLog(description = "保存资源记录")
	public String saveRoleResources(@RequestParam("roleId") Long roleId, @RequestParam("resourcesId") String resourcesId){
		SysRoleResources srr = new SysRoleResources();
		srr.setRoleId(roleId);
		srr.setResourcesIds(resourcesId);
		roleResourcesService.addRoleResources(srr);
		return String.format(Constants.REDIRECT, "sys/" + ENTITYNAME);
	}

}
